package com.asdhawaii.ics111game.item;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests the Item class.
 * 
 * @author Branden Ogata
 *
 */
public class TestItem
{
  /**
   * Tests the methods on various Item instances.
   * 
   */
  
  @Test
  public void test()
  {
    // The Item instances to test
    Item backpack = new Item("backpack", "A cloth bag for carrying items.", 
                             "This bag is primarily designed for carrying books, though anything" +
                             "else that can fit would also work.", false, false, true, 50.00, 1.0, 
                             1250.0, true);
    Item textbook = new Item("textbook", "A textbook.", 
                             "This book is large, heavy, and a good sedative.", false, false, true,
                             128.00, 10.00, 200.0, false);
    Item wallet = new Item("wallet", "A leather wallet.", 
                           "This wallet is old but still functional.", false, false, true, 25.00, 
                           1.0, 5.0, true);
    Item pizza = new Item("pizza", "A slice of pizze.", "A delicious slice of pizza.", true, false,
                          true, 4.09, 2.0, 10.0, false);
    Item soda = new Item("soda", "A can of soda.", "A red can of soda.", false, true, true, 1.50, 
                         3.0, 100.0, false);
    Item desktop = new Item("desktop computer", "A desktop computer.", 
                            "This computer is very old and is beginning to fall apart.", false,
                            false, false, 500.00, 20.0, 1500.0, false);
    Item money = new Item("money", "A small amount of money.", 
                          "The money is a mixture of one and five dollar bills.", false, false, 
                          true, 32.00, 1.0, 12.0, false);
    Item backpackClone = new Item("backpack", "A fabric bag for carrying items.", 
                                  "This bag is primarily designed for carrying supplies.", false, 
                                  false, true, 50.00, 1.0, 1250.0, true);

    // A String for use in testing the contents of the backpack
    String testString = String.format("textbook%nwallet%n  money%n");
        
    // Test all of the accessor methods
    assertEquals("Check getName", backpack.getName(), "backpack");
    assertEquals("Check getShortDescription", textbook.getShortDescription(), 
                 "A textbook.");
    assertEquals("Check getLongDescription", wallet.getLongDescription(), 
                 "This wallet is old but still functional.");
    assertTrue("Check isEdible for true", pizza.isEdible());
    assertFalse("Check isEdible for false", backpack.isEdible());
    assertTrue("Check isDrinkable for true", soda.isDrinkable());
    assertFalse("Check isDrinkable for false", textbook.isDrinkable());
    assertTrue("Check isMovable for true", wallet.isMovable());
    assertFalse("Check isMovable for false", desktop.isMovable());
    assertEquals("Check getValue", backpack.getValue(), 50.00, 0.01);
    assertEquals("Check getMass", textbook.getMass(), 10.00, 0.01);
    assertEquals("Check getVolume", wallet.getVolume(), 5.0, 0.01);
    assertTrue("Check isContainer for true", backpack.isContainer());
    assertFalse("Check isContainer for false", textbook.isContainer());
    
    // Test all of the mutator methods
    textbook.updateValue(-16.00);
    assertEquals("Check updateValue", textbook.getValue(), 112.00, 0.01);
    
    // Test the methods dealing with the contents of the Item
    // Put the textbook and wallet inside the backpack,
    // and the money inside the wallet
    backpack.addItem(textbook);
    backpack.addItem(wallet);
    wallet.addItem(money);
    assertEquals("Check getContents with contents", backpack.getContents(), 
                 testString);
    
    // Remove the wallet from the backpack
    backpack.removeItem("wallet");
    testString = String.format("textbook%n");
    assertEquals("Check getContents without wallet", backpack.getContents(),
                 testString);
    
    // Test comparisons
    assertTrue("Check equality with equals: same object", backpack.equals(backpack));
    assertTrue("Check equality with equals: same name", backpack.equals(backpackClone));
    assertFalse("Check inequality with equals: different names", textbook.equals(wallet));
    assertEquals("Check equality with compareTo: same object", backpack.compareTo(backpack), 0);
    assertEquals("Check equality with compareTo: same name", backpack.compareTo(backpackClone), 0);
    assertTrue("Check inequality with compareTo: less than", pizza.compareTo(soda) < 0);
    assertTrue("Check inequality with compareTo: greater than", money.compareTo(desktop) > 0);
    
    // Test hashcode
    assertEquals("Check equality with hashCode: same object", backpack.hashCode(), 
                 backpack.hashCode());
    assertEquals("Check equality with hashCode: same name", backpack.hashCode(), 
                 backpackClone.hashCode());
    
  }

}
